package com.it.park.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.MsgService;
import com.it.park.client.service.UserMsgService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.common.SessionUtil;
import com.it.park.constant.Constant;
import com.it.park.model.Msg;
import com.it.park.model.UserMsg;
import com.it.park.rms.client.model.User;
import com.it.park.rms.client.service.UserService;

@Controller
@RequestMapping("/admin/msg")
public class AdminMsgController {
	private String rootPath=Constant.ADMIN_PATH;

	private Page page = null;
	@Autowired
	private MsgService msgService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserMsgService userMsgService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(HttpServletRequest request){
			Integer pageNow = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> map_ = new HashMap<String, Object>();
			Integer total = msgService.selectTotal(map_);
			page = new Page(pageNow,total,rows);
			map_.put("startPos", page.getStartPos());
			map_.put("pageSize", page.getPageSize());
			List<Msg> list = msgService.selectPagination(map_);
			Map<String,Object> map  =new HashMap<String, Object>();
			map.put("total", total);
			map.put("rows", list);
			return map;
	}
	
	@RequestMapping("msgList")
	public String newsList(HttpServletRequest request){
			return rootPath+"msg/msgList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public JsonObj save(Msg msg,HttpServletRequest request,HttpSession session){
		JsonObj json = new JsonObj();
		msg.setCreateDate(new Date());
		msg.setUserId(SessionUtil.getUserID(session));
		boolean success = msgService.insertAndGetInsertID(msg);
		if(success){
			json.setMessage("添加成功!");
			json.setSuccess(true);
			//给所有用户发消息
			List<User> list = userService.selectAll();
			for (User user : list) {
				UserMsg userMsg = new UserMsg();
				userMsg.setStatus(0);
				userMsg.setUserId(user.getId());
				userMsg.setMsgId(msg.getId());
				userMsgService.insert(userMsg);
			}
		}else{
			json.setSuccess(false);
			json.setMessage("添加失败!");
		}
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj deleteById(@RequestParam("id") Integer id,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = msgService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("删除失败!");
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj edit(Msg msg,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = msgService.updateByPrimaryKeySelective(msg);
		if(success){
			json.setMessage("修改成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("修改失败!");
			json.setSuccess(false);
		}
		return json;
	}
}
